home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group99a.txt / 000227_icon-group-sender _Fri Nov 5 11:00:16 1999.msg < prev    next >
Internet Message Format  |  2000-09-20  |  3KB

  1. Return-Path: <icon-group-sender>
  2. Received: (from root@localhost)
  3.     by baskerville.CS.Arizona.EDU (8.9.1a/8.9.1) id KAA22015
  4.     for icon-group-addresses; Fri, 5 Nov 1999 10:58:32 -0700 (MST)
  5. Message-Id: <199911051758.KAA22015@baskerville.CS.Arizona.EDU>
  6. X-Authentication-Warning: agate.berkeley.edu: news set sender to <news> using -f
  7. From: Steve Wampler <swampler@noao.edu>
  8. X-Newsgroups: comp.lang.icon
  9. Subject: Re: How do I create an array of records??
  10. Date: Fri, 05 Nov 1999 08:17:34 -0700
  11. X-Trace: noao.edu 941815056 33041 140.252.38.6 (5 Nov 1999 15:17:36 GMT)
  12. X-Complaints-To: abuse@noao.edu
  13. X-Accept-Language: en
  14. To: icon-group@optima.CS.Arizona.EDU
  15. Errors-To: icon-group-errors@optima.CS.Arizona.EDU
  16. Status: RO
  17.  
  18. Andrew McGhie wrote:
  19. > I've just started programming with Icon and was wondering how to create
  20. > an array of records, for instance a database of people.
  21. > I've declared a record as follows:
  22. >    record Person(surname, forename, age, height)
  23. > but what do I do after this?  Or is there a better was to do it?
  24.  
  25. Hmmm, you have lots of ways to go at this point.  For example,
  26. if you really just want an array of records, use a list:
  27.  
  28.     db := list()        # create the 'array' (db declared global)
  29.  
  30.     procedure addPerson(surname, forename, age, height)
  31.         put(db, Person(surname, forename, age, height))
  32.     end
  33.  
  34. However, it's more likely you'll want to have some way to quickly
  35. look up entries in the database, so you might prefer to have
  36. several tables for quickly locating items using different keys:
  37.  
  38.     dbNames  := table()
  39.     dbAge    := table()
  40.     dbHeight := table()
  41.  
  42. (Note for the more Icon literate - this could be written as:
  43.  
  44.     every (dbNames | dbAge | dbHeight) := table()
  45.  
  46. but not as:
  47.  
  48.     dbNames := dbAge := dbHeight := table()
  49.  
  50. do you see why?)
  51.  
  52.     procedure addPerson(surname, forename, age, height)
  53.         person := Person(surname, forename, age, height)
  54.         /(dbNames[surname]) := []     # Use lists to allow
  55.         /(dbAge[age]) := []           #    for collisions
  56.         /(dbHeight[height]) := []
  57.         put(dbNames[surname], person)
  58.         put(dbAge[age], person)
  59.         put(dbHeight[height], person)
  60.     end
  61.  
  62. Of course, if you're writing a "real" application, you would want
  63. something considerably more sophisticated and with some sanity
  64. checks along the way - and probably want to separate the
  65. database 'operations' [store, search, retrieve, etc] from the
  66. actual items being stored, as in:
  67.  
  68.     procedure addPerson(surname, forename, age, height)
  69.         person := Person(surname, forename, age, height)
  70.         dbStore(person)
  71.     end
  72.  
  73.   
  74.  
  75. --
  76. Steve Wampler-  SOLIS Project, National Solar Observatory
  77. swampler@noao.edu
  78.